

package com.webmarmun;

import java.awt.Component;
import java.awt.Image;
import java.awt.Toolkit;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

public class Sudija extends javax.swing.JFrame {

    public String serverName = "localhost";
    public String mydatabase = "porotnici";
    public String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8";
    public String username = "root";
    public String password = "";
    public Sudija() {
        initComponents();
        java.net.URL url1 = ClassLoader.getSystemResource("com/webmarmun/camera.png");
        Toolkit kit = Toolkit.getDefaultToolkit();
        Image img = kit.createImage(url);
        this.setIconImage(img);
    }

    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ime = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Додавање судије");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(27, 19, -1, -1));

        jLabel2.setText("Име и презиме:");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 80, -1, -1));
        jPanel1.add(ime, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 80, 240, 30));

        jButton2.setBackground(new java.awt.Color(255, 51, 51));
        jButton2.setForeground(new java.awt.Color(255, 255, 255));
        jButton2.setText("Одустани");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 170, -1, 40));

        jButton1.setBackground(new java.awt.Color(102, 255, 102));
        jButton1.setText("Сними");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 170, 90, 40));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, 450, 280));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-502)/2, (screenSize.height-363)/2, 502, 363);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
}//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Component frame1 = null;
        int b = 0;
        if(ime.getText().length() == 0  ){
            JOptionPane.showMessageDialog(frame1, "Грешка !\n Морате попунити сва поља !", "Први основни суд у Београду", JOptionPane.WARNING_MESSAGE);
        }else{
            try {
                java.sql.Connection connection = null;
                connection = DriverManager.getConnection(url, username, password);
                java.sql.PreparedStatement im = connection.prepareStatement("SELECT * from sudije where Prezime_Ime_Sudije=?");
                im.setString(1,ime.getText());
                ResultSet rs = null;
                rs = im.executeQuery();
                while(rs.next()){
                    b++;
                }
                if(b==0){
                    java.sql.PreparedStatement i = connection.prepareStatement("INSERT INTO porotnik (Prezime_Ime_Sudije) values(?)");
                    i.setString(1, ime.getText().toString());
                  
                    i.executeUpdate();
                    this.dispose();
                }else{
                    JOptionPane.showMessageDialog(frame1, "Грешка !\n Поротник са истим именом већ постоји у бази !", "Први основни суд у Београду", JOptionPane.WARNING_MESSAGE);
                }

            }catch (SQLException ex){
                Logger.getLogger(Izmena.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
}//GEN-LAST:event_jButton1ActionPerformed

   
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Sudija().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField ime;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
